<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH . 'controllers/admin/application.php');

class Site_album extends Application{
    public function __construct(){
        parent::__construct();
        
        if(!$this->my_auth->is_Admin()){
            redirect(base_url().$this->lang->lang()."/admin/wb_verify/login");
            exit();
        }elseif(!$this->my_auth->is_Access($this->router->fetch_class())){
            redirect(base_url().$this->lang->lang()."/admin/manage");
            exit();
        }
        $this->load->model(array("msite_album","msite_album_menu"));
    }
    
    function _getOptionMenuAlbum($pid, $prefix="", $active=""){
        $menu_list = $this->msite_album_menu->getPidStatusData($pid, 1, "ASC", 99, 0);
		$slt = "";
        foreach($menu_list as $row){
            if($active == $row['ID']){
                $slt .= "<option value=\"".$row['ID']."\" selected=\"selected\">".$prefix." ".$row['Name'];
            }else{
                $slt .= "<option value=\"".$row['ID']."\">".$prefix." ".$row['Name'];
            }
			$slt .= $this->_getOptionMenuAlbum($row['ID'], $prefix.$prefix.$prefix.$prefix, $active);
        }
		return $slt;
    }
    public function index(){
        $data['breadcrumbs'][0]["Url"] = $this->lang->lang()."/admin/manage";
        $data['breadcrumbs'][0]["Name"] = lang('backend.homepage');
        $data['breadcrumbs'][1]["Url"] = $this->lang->lang()."/admin/site_album";
        $data['breadcrumbs'][1]["Name"] = lang('backend.album');
        
        $config['base_url'] = base_url($this->lang->lang()."/admin/site_album/index");
        $config['total_rows'] = $this->msite_album->countAllData();
        $config['per_page'] = 10;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        
        $data['name'] = "";
        $data['menu_list'] = $this->_getOptionMenuAlbum(0, "--");
        $data['album_list'] = $this->msite_album->getAllData("DESC", $config['per_page'], $this->uri->segment(5));
        
        $this->my_layout->view("backend/site_album-index",$data);
    }
    public function pid($pid){
        if(is_numeric($pid)){
            $data['check_album'] = $this->msite_album->getDataByID($pid);
            if($data['check_album']){
                $data['breadcrumbs'][0]["Url"] = $this->lang->lang()."/admin/manage";
                $data['breadcrumbs'][0]["Name"] = lang('backend.homepage');
                $data['breadcrumbs'][1]["Url"] = $this->lang->lang()."/admin/site_album";
                $data['breadcrumbs'][1]["Name"] = lang('backend.album');
                $data['breadcrumbs'][2]["Url"] = $this->lang->lang()."/admin/site_album/pid/".$data['check_album']['ID'];
                $data['breadcrumbs'][2]["Name"] = $data['check_album']['Name'];
                
                $config['base_url'] = base_url($this->lang->lang()."/admin/site_album/pid/$pid");
                $config['total_rows'] = $this->msite_album->countMenuData($pid);
                $config['per_page'] = 10; 
                $config['uri_segment'] = 6;
                $this->pagination->initialize($config);
                
                $data['name'] = "";
                $data['menu_list'] = $this->_getOptionMenuAlbum(0, "--");
                $data['album_list'] = $this->msite_album->getMenuData($pid, "DESC", $config['per_page'], $this->uri->segment(6));
                
                $this->my_layout->view("backend/site_album-index",$data);
            }else{
                redirect(base_url().$this->lang->lang()."/admin/site_album");
                exit();
            }
        }else{
            redirect(base_url().$this->lang->lang()."/admin/site_album");
            exit();
        }
    }
    public function search($name="", $menu=""){
        $data['breadcrumbs'][0]["Url"] = $this->lang->lang()."/admin/manage";
        $data['breadcrumbs'][0]["Name"] = lang('backend.homepage');
        $data['breadcrumbs'][1]["Url"] = $this->lang->lang()."/admin/site_album";
        $data['breadcrumbs'][1]["Name"] = lang('backend.album');
        $data['breadcrumbs'][2]["Url"] = "#";
        $data['breadcrumbs'][2]["Name"] = lang('backend.search');
        
        $data['name'] = $this->input->post("name");
        $data['menu'] = $this->input->post("menu");
        if(!$data['name'] && $name){
            $data['name'] = urldecode($name);
        }
        if(!$data['menu'] && $menu){
            $data['menu'] = urldecode($menu);
        }
        
        $config['base_url'] = base_url($this->lang->lang()."/admin/site_album/search/{$data['name']}/{$data['menu']}");
        $config['total_rows'] = $this->msite_album->countSearchData($data['name'], $data['menu']);
        $config['per_page'] = 10; 
        $config['uri_segment'] = 7;
        $this->pagination->initialize($config);
        
        $data['menu_list'] = $this->_getOptionMenuAlbum(0, "--",$data['menu']);
        
        $data['album_list'] = $this->msite_album->getSearchData($data['name'], $data['menu'], "DESC", $config['per_page'], $this->uri->segment(7));
        $this->my_layout->view("backend/site_album-index",$data);
    }
    
    public function add(){
        $data['breadcrumbs'][0]["Url"] = $this->lang->lang()."/admin/manage";
        $data['breadcrumbs'][0]["Name"] = lang('backend.homepage');
        $data['breadcrumbs'][1]["Url"] = $this->lang->lang()."/admin/site_album";
        $data['breadcrumbs'][1]["Name"] = lang('backend.album');
        $data['breadcrumbs'][2]["Url"] = "#";
        $data['breadcrumbs'][2]["Name"] = lang('backend.add');
        
        $data['album_menu'] = $this->_getOptionMenuAlbum(0,"--");
        $data['album_list'] = $this->msite_album->getAllData("DESC", 10, 0);
        
        $this->form_validation->set_rules("menu",lang('backend.menu'),"required");
        $this->form_validation->set_rules("name",lang('backend.name'),"required");
        $this->form_validation->set_rules("img",lang('backend.img'),"required");
        $this->form_validation->set_rules("maincontent",lang('backend.maincontent'),"required|min_length[10]");
        $this->form_validation->set_rules("content",lang('backend.content'),"required|min_length[10]");
        if($this->form_validation->run() == FALSE){
            $data['error'] = ""; 
            $this->my_layout->view("backend/site_album-add",$data);
        }else{
            $thumb = $this->input->post("img");
            if(strrpos("wb".$this->input->post("img"),base_url())){
                if(_checkThumbImg($this->input->post("img"))){
                    $thumb = _checkThumbImg($this->input->post("img"));
                }
            }
            $add = array(
                    "Menu"          => $this->input->post("menu"),
                    "Name"          => $this->input->post("name"),
                    "Img"           => $this->input->post("img"),
                    "Thumb1"        => $thumb,
                    "Thumb2"        => "",
                    "Thumb3"        => "",
                    "Thumb4"        => "",
                    "MainContent"   => $this->input->post("maincontent"),
                    "Content"       => $this->input->post("content"),
                    "Dateset"       => date("Y-m-d H:i:s"),
                    "Hot"           => $this->input->post("hot"),
                    "New"           => $this->input->post("new"),
                    "Hit"           => 1,
                    "Status"        => $this->input->post("status"),
                    "MTit"          => $this->input->post("mtit"),
                    "MKey"          => $this->input->post("mkey"),
                    "MDes"          => $this->input->post("mdes"),
                 );
            $this->msite_album->addData($add);
            redirect(base_url().$this->lang->lang()."/admin/site_album");
            exit();
        }
    }
    public function edit($id){
        if(is_numeric($id)){            
            $data['album_check'] = $this->msite_album->getDataByID($id);
            if($data['album_check']){
                $data['breadcrumbs'][0]["Url"] = $this->lang->lang()."/admin/manage";
                $data['breadcrumbs'][0]["Name"] = lang('backend.homepage');
                $data['breadcrumbs'][1]["Url"] = $this->lang->lang()."/admin/site_album";
                $data['breadcrumbs'][1]["Name"] = lang('backend.album');
                $data['breadcrumbs'][2]["Url"] = $this->lang->lang()."/admin/site_album/edit/".$data['album_check']['ID'];
                $data['breadcrumbs'][2]["Name"] = $data['album_check']['Name'];
                
                $data['album_menu'] = $this->_getOptionMenuAlbum(0,"-", $data['album_check']['Menu']); 
                $data['album_list'] = $this->msite_album->getAllData("DESC", 10, 0);
                
                $this->form_validation->set_rules("menu",lang('backend.menu'),"required");
                $this->form_validation->set_rules("name",lang('backend.name'),"required");
                $this->form_validation->set_rules("img",lang('backend.img'),"required");
                $this->form_validation->set_rules("maincontent",lang('backend.maincontent'),"required|min_length[10]");
                $this->form_validation->set_rules("content",lang('backend.content'),"required|min_length[10]");
            if($this->form_validation->run() == FALSE){
                $data['error'] = ""; 
                $this->my_layout->view("backend/site_album-edit",$data);
            }else{
                $thumb = $this->input->post("img");
                if(strrpos("wb".$this->input->post("img"),base_url())){
                    if(_checkThumbImg($this->input->post("img"))){
                        $thumb = _checkThumbImg($this->input->post("img"));
                    }
                }
                $update = array(
                        "Menu"          => $this->input->post("menu"),
                        "Name"          => $this->input->post("name"),
                        "Img"           => $this->input->post("img"),
                        "Thumb1"        => $thumb,
                        "Thumb2"        => $data['album_check']['Thumb2'],
                        "Thumb3"        => $data['album_check']['Thumb3'],
                        "Thumb4"        => $data['album_check']['Thumb4'],
                        "MainContent"   => $this->input->post("maincontent"),
                        "Content"       => $this->input->post("content"),
                        "Dateset"       => $data['album_check']['Dateset'],
                        "Hot"           => $this->input->post("hot"),
                        "New"           => $this->input->post("new"),
                        "Hit"           => $data['album_check']['Hit'],
                        "Status"        => $this->input->post("status"),
                        "MTit"          => $this->input->post("mtit"),
                        "MKey"          => $this->input->post("mkey"),
                        "MDes"          => $this->input->post("mdes"),
                     );
                    $this->msite_album->updateData($update, $id);
                    redirect(base_url().$this->lang->lang()."/admin/site_album");
                    exit();
                }
            }else{
                redirect(base_url().$this->lang->lang()."/admin/site_album");
                exit();
            }
        }else{
            redirect(base_url().$this->lang->lang()."/admin/site_album");
            exit();
        }
    }
    public function delete($id){
        if(is_numeric($id)){
            $data['album_check'] = $this->msite_album->getDataByID($id);
            if($data['album_check']){
                $this->msite_album->deleteData($id);
            }
        } 
        redirect(base_url().$this->lang->lang()."/admin/site_album");
        exit();
    }
        
}